package com.achun.getcouponleague.base;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.achun.getcouponleague.Bean.CategoriesContent;
import com.achun.getcouponleague.R;
import com.achun.getcouponleague.utils.Logutils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class LooperViewpageAdapter extends PagerAdapter {
   private List<CategoriesContent.DataBean> viewpages=new ArrayList<>();

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        int realpositon=position%viewpages.size();
//根据控件大小设置图片大小
//        ViewGroup.LayoutParams layoutParams1 = container.getLayoutParams();
//        int height = layoutParams1.height;
//        int width = layoutParams1.width;
//        int size=height>width?height:width;
        String coverUrl="https:"+ viewpages.get(realpositon).getPict_url();
        ImageView img=new ImageView(container.getContext());
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        img.setLayoutParams(layoutParams);
        img.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(container.getContext()).load(coverUrl).into(img);
        container.addView(img);
        //点击事件
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (looperClicklistener!=null)
                looperClicklistener.onClick(realpositon,viewpages.get(realpositon));
            }
        });
        return img;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }



    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {

        return view==object;
    }

    public void setdatas(List<CategoriesContent.DataBean> categorypagelist) {
Logutils.e(this.getClass(),"轮播图适配器数据--》"+categorypagelist.get(0).getPict_url());
        this.viewpages.clear();

        viewpages.addAll(categorypagelist);
        notifyDataSetChanged();
    }

    public int getDataSize() {
        return viewpages.size();
    }
    public interface LooperClicklistener{
        void onClick(int position,CategoriesContent.DataBean item);
    }
    private LooperClicklistener looperClicklistener;
    public void setLooperClicklistener(LooperClicklistener looperClicklistener){
        this.looperClicklistener=looperClicklistener;
    }
}
